package cn.edu.zafu.bigdata.LiJuan.ch12;

import java.sql.SQLException;
import java.util.Scanner;

public class StudentInfoSystem {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("======= 学生信息管理系统 =======");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    try {
                        addStudent(scanner);
                    } catch (SQLException e) {
                        System.out.println("数据库操作失败：" + e.getMessage());
                    }
                    break;
                case 2:
                    try {
                        retrieveStudent(scanner);
                    } catch (SQLException e) {
                        System.out.println("数据库操作失败：" + e.getMessage());
                    }
                    break;
                case 3:
                    try {
                        updateStudent(scanner);
                    } catch (SQLException e) {
                        System.out.println("数据库操作失败：" + e.getMessage());
                    }
                    break;
                case 4:
                    try {
                        deleteStudent(scanner);
                    } catch (SQLException e) {
                        System.out.println("数据库操作失败：" + e.getMessage());
                    }
                    break;
                case 5:
                    System.out.println("程序退出。");
                    scanner.close();
                    System.exit(0);
                    break;
                default:
                    System.out.println("无效的输入，请重新输入。");
                    break;
            }
        }
    }

    private static void addStudent(Scanner scanner) throws SQLException {
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();
        System.out.print("请输入姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入电话: ");
        String phone = scanner.nextLine();
        Student student = new Student(id, name, phone);
        if (DatabaseManager.retrieveStudent(id) != null) {
            System.out.println("学号重复，请重新输入。");
            return;
        }
        DatabaseManager.addStudent(student);
        System.out.println("学生信息添加成功。");
    }

    private static void retrieveStudent(Scanner scanner) throws SQLException {
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();
        Student student = DatabaseManager.retrieveStudent(id);
        if (student != null) {
            System.out.println("学生信息：" + student);
        } else {
            System.out.println("未找到该学生。");
        }
        System.out.print("继续检索？(输入'QUIT'退出): ");
        String continueSearch = scanner.nextLine();
        if (!continueSearch.equalsIgnoreCase("QUIT")) {
            retrieveStudent(scanner);
        }
    }

    private static void updateStudent(Scanner scanner) throws SQLException {
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();
        Student student = DatabaseManager.retrieveStudent(id);
        if (student == null) {
            System.out.println("未找到该学生。");
            return;
        }
        System.out.print("请输入新的姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入新的电话: ");
        String phone = scanner.nextLine();
        student.setName(name);
        student.setPhone(phone);
        DatabaseManager.updateStudent(student);
        System.out.println("学生信息更新成功。");
        System.out.print("继续更新？(输入'QUIT'退出): ");
        String continueUpdata = scanner.nextLine();
        if (!continueUpdata.equalsIgnoreCase("QUIT")) {
            updateStudent(scanner);
        }
    }

    private static void deleteStudent(Scanner scanner) throws SQLException {
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();
        if (DatabaseManager.retrieveStudent(id) == null) {
            System.out.println("未找到该学生。");
            return;
        }
        DatabaseManager.deleteStudent(id);
        System.out.println("学生信息删除成功。");
        System.out.print("继续删除？(输入'QUIT'退出): ");
        String continueDelete = scanner.nextLine();
        if (!continueDelete.equalsIgnoreCase("QUIT")) {
            deleteStudent(scanner);
        }
    }
}